package com.model.imp;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.model.dao.StudentDao;
import com.model.entity.Student;
import com.model.util.DBUtil;
import com.model.util.HibernateUtil;


public class StudentImp implements StudentDao {
         private Connection conn;
	     private PreparedStatement ps;
	     private ResultSet rs;
	     private String findByNamehql="from Student where stuname=?";
	     private String getAllStudent= "from Student where state=?";
	     private String getTatol="select count(*) from Student where state=?";
	     private Session session=null;
	     private Transaction ta=null;
	public boolean add(Student student) {
		try {
		session=HibernateUtil.getSessionFactory().getCurrentSession();
    	ta=session.beginTransaction();
   
    	session.save(student);
    	ta.commit();
		return true;
		}catch (Exception e){
			if(ta!=null)
				ta.rollback();
			
			e.printStackTrace();
		}finally {
			HibernateUtil.closeSession();
		}

		return false;
		
	}
     // 根据姓名查询
	public Student findByName(String stuname) {
		// TODO Auto-generated method stub
	      try{  session=HibernateUtil.getSessionFactory().getCurrentSession();
		    ta=session.beginTransaction();
		    Query query=session.createQuery(findByNamehql);
		    query.setString(0, stuname);
		    Student student=(Student)query.uniqueResult();
		    if(student!=null){
			Student s=new Student(student.getStuname(),student.getPassword(),student.getStunumber(),student.getBirthday(),student.getSex(),student.getStuPolity(),student.getStuBrief(),student.getType(),student.getState());
			s.setBirthday(student.getBirthday());
			return s;
		    }
	      }catch (Exception e){
	    	  if(ta!=null)
	    		  ta.rollback();
	    	  e.printStackTrace();
	      }finally{
	    	  HibernateUtil.closeSession();
	      } 
        	return null;
	}
       //根据学号查询
	public Student findByNumber(String stuNumber) {
		try{
			String findByNamehql="from Student where stunumber=?";
		    Session session=HibernateUtil.getSessionFactory().getCurrentSession();
		    Transaction ta=session.beginTransaction();
		    Query query=session.createQuery(findByNamehql);
		    query.setString(0, stuNumber);
		    Student student=(Student)query.uniqueResult();
		    if(student!=null){
			Student s=new Student(student.getStuname(),student.getPassword(),student.getStunumber(),student.getBirthday(),student.getSex(),student.getStuPolity(),student.getStuBrief(),student.getType(),student.getState());
			s.setBirthday(student.getBirthday());
			return s;
		      }
	      
		   }catch (Exception e){
	    	  if(ta!=null)
	    		 
	    		  ta.rollback();
	    	  e.printStackTrace();
	      }finally{
	    	  HibernateUtil.closeSession();
	      } 
     	return null;
	}
	
	
	public Student findById(int id) {
		try{
			String findByNamehql="from Student where id=?";
		    Session session=HibernateUtil.getSessionFactory().getCurrentSession();
		    Transaction ta=session.beginTransaction();
		    Query query=session.createQuery(findByNamehql);
		    query.setInteger(0, id);
		    Student student=(Student)query.uniqueResult();
		    if(student!=null){
			Student s=new Student(student.getStuname(),student.getPassword(),student.getStunumber(),student.getBirthday(),student.getSex(),student.getStuPolity(),student.getStuBrief(),student.getType(),student.getState());
			s.setBirthday(student.getBirthday());
			return s;
		      }
	      
		   }catch (Exception e){
	    	  if(ta!=null)
	    		 
	    		  ta.rollback();
	    	  e.printStackTrace();
	      }finally{
	    	  HibernateUtil.closeSession();
	      } 
     	return null;
	}
	
    //根据学号和姓名查询
	public Student findByStudent(String stuNumber,String stuName) {
        try{
        	String findByStudenthql="from Student where stunumber=? and stuname=?";
            session=HibernateUtil.getSessionFactory().getCurrentSession();
 		    ta=session.beginTransaction();
 		    Query query=session.createQuery(findByStudenthql);
 		    query.setString(0, stuNumber);
 		    query.setString(1, stuName);
 		    Student student=(Student)query.uniqueResult();
		    if(student!=null){
			Student s=new Student(student.getStuname(),student.getPassword(),student.getStunumber(),student.getBirthday(),student.getSex(),student.getStuPolity(),student.getStuBrief(),student.getType(),student.getState());
			s.setBirthday(student.getBirthday());
			return s;
		      }
	      
		   }catch (Exception e){
	    	  if(ta!=null)
	    		  ta.rollback();
	    	  e.printStackTrace();
	      }finally{
	    	  HibernateUtil.closeSession();
	      } 
  	       return null;
	}
	
	
	//查询所有学生
	public List<Student> getStudent() {
		// TODO Auto-generated method stub
		 List<Student> list=null;
		  try{
			
			session=HibernateUtil.getSessionFactory().getCurrentSession();
	        ta=session.beginTransaction();
		    Query query=session.createQuery(getAllStudent);
		    query.setInteger(0, 1);
		    list=query.list(); 
		    ta.commit();
		  }catch(Exception e){
			if(ta!=null)
				ta.rollback();
			    e.printStackTrace();
		  }finally{	
			HibernateUtil.closeSession();
		   }
		
		return list;
	}

//删除用户 状态 1 表示正常  0 表示删除    2 表示毕业       当state=0时就等于删除了！
	 public boolean delStudent(Student student){
		    String delStudentsql="update Student set state=? where stunumber=?";
		     Session session=HibernateUtil.getSessionFactory().getCurrentSession();
		     Transaction ta=session.beginTransaction();
		     Query query=session.createQuery(delStudentsql);
            query.setInteger(0, 0);
            query.setString(1, student.getStunumber());
		    int i =query.executeUpdate();
             ta.commit();
		   if(i>0){ return true;}
		   else{ return false;}
	  }
	  public boolean delTrueStudent(Student student){
		    Session session=HibernateUtil.getSessionFactory().getCurrentSession();
		    Transaction ta=session.beginTransaction();
		    session.delete(student);
		    ta.commit();
		    return true;
		   
     
	  }


  //修改student
	   public boolean update(Student student) {
	          String updatesql=" update person(stuname,password,stunumber,birthday,sex,stuPolity,stuBrief,type,state) values(?,?,?,?,?,?,?,?,?)";
		      Session session =HibernateUtil.getSessionFactory().getCurrentSession();
			  Transaction ta=session.beginTransaction();
			  Query query=session.createQuery(updatesql);
				query.setString(1, student.getStuname());
				query.setString(2, student.getPassword());
				query.setString(3, student.getStunumber());
				query.setDate(4, student.getBirthday());
				query.setInteger(5, student.getSex());
				query.setInteger(6, student.getStuPolity());
				query.setString(7, student.getStuBrief());
				query.setInteger(8, student.getType());
				query.setInteger(9, student.getState());
	            if(query.executeUpdate()>0){
	            	return true;
	            }
		         return false;
	}

	   public int getTotal() {
			session=HibernateUtil.getSessionFactory().getCurrentSession();
			ta=session.beginTransaction();
			Query query=session.createQuery(getTatol);
			query.setInteger(0, 1);
		    System.out.println(query.uniqueResult());
		    long count=(Long)query.uniqueResult();
			ta.commit();
			return (int)count;
		}

	public Collection<Student> getStudents(int pageStartRow, int pageSize) {
		List<Student> list = new ArrayList<Student>();
		session=HibernateUtil.getSessionFactory().getCurrentSession();
		ta=session.beginTransaction();
		Query query=session.createQuery(getAllStudent);
		query.setInteger(0, 1);
		query.setFirstResult(pageStartRow);
		query.setMaxResults(pageSize);
		list=query.list();
		ta.commit();
		return list;
	}



}
